package com.xiaoq.matrix.minipro.service.impl;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;


public class MiniProQlCodeServiceImpl {
	
	private static final Logger LOG = LoggerFactory.getLogger(MiniProQlCodeServiceImpl.class);

	public static String getminiqrQr(String accessToken, String path) {
		RestTemplate rest = new RestTemplate();
		String base64Image = null;
//		pages/index/index 小程序首页路径
//   	pages/orderPerson/orderPerson 购买门票路径
//   	pages/page10009/page10009 讲解员路径
		String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken;
		Map<String, Object> param = new HashMap<>();
		param.put("path", path);
		param.put("width", 430);
		param.put("auto_color", false);
		Map<String, Object> line_color = new HashMap<>();
		line_color.put("r", 0);
		line_color.put("g", 0);
		line_color.put("b", 0);
		param.put("line_color", line_color);
		MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
		HttpEntity requestEntity = new HttpEntity(param, headers);
		ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
		byte[] result = entity.getBody();
		base64Image = Base64.encodeBase64String(result);
		return base64Image;    
	    }
	 
	 
	 
	    /**
	     * 干嘛自己去拼呢提醒景区申请小程序是上传自己的logo不就好了吗
	     * @param qlCodePath 二维码的路径
	     * @param logoPath 景区logo路径
	     * @param txt 二维码上的字
	     */
	    public static final void overlapImage(String qlCodePath, String logoPath) {
		   
	    	try {
		      File bigFile = new File(qlCodePath);
		      File smallFile = new File(logoPath);
		      BufferedImage small = ImageIO.read(smallFile);
		      BufferedImage big = ImageIO.read(bigFile);
		      System.out.println("大"+big.getWidth()+"大高"+big.getHeight());
		      System.out.println("小"+small.getWidth()+"小"+small.getHeight());
		      int x = (big.getWidth()-small.getWidth())/2;
		      int y = (big.getHeight()-small.getHeight())/2;
		      System.out.println("x"+x+"y"+y);
		      Graphics2D g = big.createGraphics();
		      g.drawImage(small, big.getWidth()*5/12,big.getHeight()*5/12,  big.getWidth()/4, big.getHeight()/4, null);
		      g.dispose();
		      ImageIO.write(big, "jpg", new File("E:\\mqlCode.jpg"));
		    } catch (Exception e) {
		      e.printStackTrace();
		    }
		  }
	    
	    public static void main(String[] args) {
//	    	String qinyan = getminiqrQr("7_eUg3hyYM_FD5oaD1__vG18dFumXA7wF1GJiCxcsYiA7N0aFRiTEorQxf-qRCQEMHYhQ1_mNd0YPiI56-VwOMqzFNqGjOoV_xqfDXoPNDCv_zA9C0ZwhmL6VOoF681qv7zint1mpnB1B1rouyPSIiAJDWZP");
//	    	String tianhe = getminiqrQr("8_Uqij2L1w69kselZV-DtPgR4bw4RQUHmXxquAIq65izcnMwl0uM4vzWvB1KgR84A67h2wlHrfcKKyye9LXvCcSuy_npUsWmANYzC69qGO233jbZjgOcpjrcJ4_B0X33oJoSLp3mmCcQbFDEctYWAbAMDNLP",1);
//	    	System.out.println(tianhe);
		}
	    
}
